package com.lsfb.signln.Utils;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lsfb.signln.R;


public class MyDialog extends Dialog {

    public MyDialog(Context context) {
        super(context);
    }

    public MyDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private View contentView;
        private String positiveButtonText;
        private String negativeButtonText;
        private String context;
        private View.OnClickListener positiveButtonClickListener;
        private View.OnClickListener negativeButtonClickListener;

        private View layout;
        private MyDialog dialog;

        public Builder(Context context) {
            //这里传入自定义的style，直接影响此Dialog的显示效果。style具体实现见style.xml
            dialog = new MyDialog(context, R.style.Dialog);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layout = inflater.inflate(R.layout.pop_my, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        }

        public Builder setMessage(String message) {
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText, View.OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText, View.OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setContext(String context) {
            this.context = context;
            return this;
        }

        public Builder setSingleButton(String singleButtonText, View.OnClickListener listener) {
            return this;
        }


        /**
         * 创建双按钮对话框
         *
         * @return
         */
        public MyDialog createTwoButtonDialog() {
            layout.findViewById(R.id.pop_call).setOnClickListener(positiveButtonClickListener);
            layout.findViewById(R.id.pop_del).setOnClickListener(negativeButtonClickListener);
            //如果传入的按钮文字为空，则使用默认的“是”和“否”
            if (positiveButtonText != null) {
                ((TextView) layout.findViewById(R.id.pop_call)).setText(positiveButtonText);
            } else {
                ((TextView) layout.findViewById(R.id.pop_call)).setText("确定");
            }
            if (negativeButtonText != null) {
                ((TextView) layout.findViewById(R.id.pop_del)).setText(negativeButtonText);
            } else {
                ((TextView) layout.findViewById(R.id.pop_del)).setText("取消");
            }
            if (context != null) {
                ((TextView) layout.findViewById(R.id.pop_text_context)).setText(context);
            } else {
                ((TextView) layout.findViewById(R.id.pop_text_context)).setText("是否删除所有信息");
            }
            create();
            return dialog;
        }

        /**
         * 单按钮对话框和双按钮对话框的公共部分在这里设置
         */
        private void create() {
            if (contentView != null) {       //如果使用Builder的setContentview()方法传入了布局，则使用传入的布局
                ((LinearLayout) layout.findViewById(R.id.content)).removeAllViews();
                ((LinearLayout) layout.findViewById(R.id.content))
                        .addView(contentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            }
            dialog.setContentView(layout);
            dialog.setCancelable(true);     //用户可以点击手机Back键取消对话框显示
            dialog.setCanceledOnTouchOutside(false);        //用户不能通过点击对话框之外的地方取消对话框显示
        }

    }
}
